<?php
declare (strict_types=1);

namespace addons\V2\admin\controller;

use addons\Builder\facade\Builder;
use app\common\model\Area;
use app\common\model\Factory;
use think\exception\ValidateException;
use think\facade\View;

class Order extends \app\admin\controller\Base
{

    //主键，默认为id
    protected $pk = 'id';

    //主模型
    protected $model = '\addons\V2\admin\model\Order';

    //关联模型
    protected $hasOne = ['user', 'express', 'p', 'c', 'd','factory'];

    protected int $takeOver = 3;

    public function index(): string
    {
        return $this->setTable([],[['order_type', '<=', 2]],1);
    }

    public function spec(): string
    {
        return $this->setTable([['factory_id', '厂家', 'select', Factory::where('delete_time',0)->column('title','id')]],[['order_type', '>', 2]],2);
    }

    public function setTable($search,$extra_map,$type)
    {
        $table = Builder::Table()
            ->addColumn(['type' => 'checkbox', 'fixed' => 'left', 'title' => '全选'])
            ->addColumn(['field' => 'id', 'title' => '订单ID', 'width' => 90])
            ->addColumn(['field' => 'sn', 'title' => '订单编号', 'width' => 170])
            ->addColumn(['field' => 'product', 'title' => '订单商品', 'width' => 350, 'templet' => '<div>{{- d.product}}</div>']);
        if($type==2)
        {
            $table->addColumn(['field' => 'factory_id', 'title' => '厂家', 'templet' => '<div>{{d.factory.title}}</div>']);
        }
        return $table->addColumn(['field' => 'total_amount', 'title' => '订单价格'])
            ->addColumn(['field' => 'real_amount', 'title' => '实际支付'])
            ->addColumn(['field' => 'code', 'title' => '下单会员', 'templet' => '#_userTpl', 'width' => 200])
            ->addColumn(['field' => 'create_time', 'title' => '下单时间', 'width' => 150])
            ->addColumn(['field' => 'receiver', 'title' => '收货人'])
            ->addColumn(['field' => 'receiver_tel', 'title' => '收货电话', 'width' => 100])
            ->addColumn(['field' => 'address', 'title' => '下单地址', 'templet' => '#addressTpl', 'width' => 250])
            ->addColumn(['field' => 'pay_time', 'title' => '支付时间', 'width' => 150])
            ->addColumn(['field' => 'pay_way', 'title' => '支付方式', 'width' => 120])
            ->addColumn(['field' => 'post_time', 'title' => '发货时间', 'width' => 150])
            ->addColumn(['field' => 'status', 'title' => '订单状态'])
            ->addColumn(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#orderDemo', 'title' => '操作', 'width' => 160])
            ->addTopButtons(['deleteAll', 'export'])
            ->addExtraMap($extra_map)
            ->addSearch(
                array_merge([
                ['uid', '会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id'],
                ['sn', '订单编号', 'input'],
                ['status', '订单状态', 'select', $this->model::getOrderStatus('', 2)]
                    ],$search
                )
            )
            ->addRightButtons([
                'edit'    => [
                    'title'       => '详情',
                    'unity_title' => '订单详情',
                    'size'        => '50%',
                    'site'        => 'r'
                ],
                'deliver' => ['title' => '发货', 'size' => '50%',
                              'url'   => url('Order/deliver')->build(),
                              'event' => '_openNewPage',
                              'site'  => 'r'
                ],
                'delete'
            ])
            ->addJsUrl(['order'])
            ->addExtraHtml(['order'])
            ->fetch();
    }

    public function edit($id = '', $tpl = '', $info = '')
    {
        return parent::edit($id, $this->app->getRootPath() . 'addons/V2/admin/view/order/edit.html', $info); // TODO: Change the autogenerated stub
    }

    public function deliver($id = '')
    {

        if ($this->request->isPost()) {
            $param = $this->request->param();
            try {
                $this->validate($param, '\addons\V2\admin\validate\OrderDeliver');
            } catch (ValidateException $e) {
                return json(['code' => -1, 'msg' => $e->getError()]);
            }
            return json($this->model::deliver($param, $this->admin));
        }

        $info = $this->model::find($id);

        View::assign([
            'express' => \app\common\model\Express::where(['status' => 1])->select(),
            'info'    => $info
        ]);
        return View::fetch($this->app->getRootPath() . 'addons/V2/admin/view/order/deliver.html');
    }

    public function getArea($id, $level = 0)
    {
        return Area::getArea($id ?: 100000, $level);
    }


}
